//int的序列化

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>

//序列化int到字节数组-大端序
void serialize_int(int value, uint8_t* buffer)
{
    uint32_t net_value = htonl((uint32_t)value);
    memcpy(buffer,&net_value,sizeof(net_value));
}

//从字节数组反序列化int-大端序
int deserialize_int(const uint8_t* buffer)
{
    uint32_t net_value;
    memcpy(&net_value,buffer,sizeof(net_value));
    return (int)ntohl(net_value);
}

int main(int argc, char const *argv[])
{
    
    int original_value = 0x12345678;
    uint8_t buffer[4];
    printf("原始值：0x%x\n",original_value);
    serialize_int(original_value,buffer);
    printf("大端序序列化：");
    for (size_t i = 0; i < sizeof(int); i++)
    {
        printf("%02x",buffer[i]);
        
    }
    printf("\n");

    int deserialize_value = deserialize_int(buffer);
    printf("大端序反序列化:0x%x\n",deserialize_value);
    

    return 0;
}



